<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const data = [{
      id: 1,
      name: '222',
      children: [{
        id: 2,
        name: '34',
        children: [{
          id: 112,
          name: '334',
        }, {
          id: 113,
          name: '354',
        }]
      }]
    }]

    function fn(id) {
      let result = []
      const find = _ => {
        if (!_) return
        return _.find(v => (v.id === id || find(v.children)) && result.push(v.id))
      }
      find(data)

      return result.reverse()
    }



    console.log(fn(112))
  </script>
</body>

</html>